package com.hackathon.sgstern.utilities;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import com.google.android.gms.maps.model.LatLng;

public class AddressUtilities {

	public static String getAddressOfPoint(LatLng position, Context context) {
		String addressText = "";
		Address address = getAddress(position, context);
		if (address != null) {
			addressText = String.format(
						"%s, %s, %s",
						// If there's a street address, add it
						address.getMaxAddressLineIndex() > 0 ? address
								.getAddressLine(0) : "",
						// Locality is usually a city
						address.getLocality(),
						// The country of the address
						address.getCountryName());
		}
		return addressText;
	}
	
	private static Address getAddress(LatLng position, Context context){
		Address address = null;
		try {
			Geocoder geoCoder = new Geocoder(context, Locale.GERMANY);
			List<Address> adresses = geoCoder.getFromLocation(
				position.latitude, position.longitude, 1);
			if (!adresses.isEmpty()) {
				address = adresses.get(0);
			}
		} catch (IOException ex) {
		}
		return address;
	}
}
